﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Text;
using Common;
 
public partial class shopping_checkout : MemberPageBase
{
    public string OrderNumber = "", OrderDate = "",  TotalPrice = "", OrderStatus = "",  OrderTypeName = "";

    protected void Page_Load(object sender, EventArgs e)
    {
        PageBegin();

        switch (RequestBase.GetFormString("otype"))
        {
            case "getaddressobj":
                InitializeAddressObj();
                break;
        }
          
        if (RequestBase.GetFormString("action") == "cancel")
        {
            CancelOrder();
        }
        string aa = RequestBase.GetFormString("___EVENTTARGET");
        if (RequestBase.GetFormString("___EVENTTARGET") == "exchangeproduct")
        { 
            ExchangeConfirm();
        }
        
        if (!IsPostBack)
        {
            string result;
            if (Utils.IsGuid(RequestBase.GetQueryString("orderid")))
            {
                Model.OrderBase _order = BLL.OrderBase.GetObjectById(RequestBase.GetQueryString("orderid"), out result);
                if (_order != null)
                {
                    OrderNumber = _order.OrderNumber;
                    TotalPrice = _order.TotalPrice.ToString();
                    OrderStatus = _order.StatusCode.ToString().Replace("-1", "已作废").Replace("0", "待支付").Replace("1", "已对换");
                    txtOrderId.Value = _order.OrderId;
                    txtShippingAddressId.Value = _order.ShippingAddressId;
                    OrderDate = _order.OrderDate.ToString("yyyy-MM-dd");

                    if (_order.StatusCode != 0)
                    {
                        panlOperator.InnerHtml = "";
                        liCharge.Style.Add("display", "none");

                        if (_order.StatusCode == -1)
                            OrderStatus += "，" + _order.CancelReason + "。";
                    }

                    emTotalCost.InnerHtml = _order.TotalPrice.ToString();

                    rptOrderItems.DataSource = _order.OrderItems;
                    rptOrderItems.DataBind();
                    _order = null;
                }
            }
        }
    }

    /// <summary>
    /// 获取送货地址信息
    /// </summary>
    void InitializeAddressObj()
    {
        Model.ShippingAddressBase _obj = BLL.ShippingAddressBase.GetObjectById(RequestBase.GetFormString("txtShippingAddressId"));
        StringBuilder GetValues = new StringBuilder();
        GetValues.Append("<?xml version=\"1.0\" encoding=\"utf-8\"?><ShippingAddressBase><fileds>");
        GetValues.Append("<filed name=\"ShipAddressName\">" + _obj.ShipAddressName + "</filed>");
        GetValues.Append("<filed name=\"ShipAddress\">" + _obj.ShipAddress + "</filed>");
        GetValues.Append("<filed name=\"ShipCounties\">" + _obj.ShipState + " | " + _obj.ShipCity + " | " + _obj.ShipCounties + "</filed>");
        GetValues.Append("<filed name=\"ShipZip\">" + _obj.ShipZip + "</filed>");
        GetValues.Append("<filed name=\"ShipTel\">" + _obj.ShipTel + "</filed>");
        GetValues.Append("<filed name=\"ShipMobile\">" + _obj.ShipMobile + "</filed>");
        GetValues.Append("<filed name=\"ShipToName\">" + _obj.ShipToName + "</filed>");
        GetValues.Append("<filed name=\"CountieId\">" + _obj.ShipCountiesId.ToString() + "</filed>");
        GetValues.Append("<filed name=\"CityId\">" + _obj.ShipCityId.ToString() + "</filed>");
        GetValues.Append("<filed name=\"ProvinceId\">" + _obj.ShipStateId.ToString() + "</filed>");
        GetValues.Append("<filed name=\"ShipCountryId\">" + _obj.ShipCountryId.ToString() + "</filed>");
        GetValues.Append("</fileds></ShippingAddressBase>");

        BLL.AjaxHelper.ResponseWriteXML(GetValues.ToString());
    }

    void CancelOrder()
    {
        int reasoncode = int.Parse(RequestBase.GetFormString("rdoCancelReason"));
        string reason = RequestBase.GetFormString("hidCancelReason");
        if (reasoncode == 10)
            reason = RequestBase.GetFormString("txtCancelReason");

        string res = BLL.OrderBase.CancelOrder(RequestBase.GetFormString("txtOrderId"), this.UserId, reasoncode, reason);

        switch (res)
        {
            case "succeeded":
                JScript.AlertAndRedirect("操作成功!", "shopping_orderlist.aspx");
                break;
            default:
                JScript.Alert(res);
                break;
        }
    }

    void ExchangeConfirm()
    {
        string res = "";
        Model.OrderBase _obj = BLL.OrderBase.GetObjectById(RequestBase.GetQueryString("orderid"), out res);
        if (_obj != null)
        {
            res = BLL.OrderBase.ExchangeConfirm(this.UserId, _obj.OrderId, int.Parse(Convert.ToString(Math.Ceiling(_obj.TotalPrice))));
        }

        switch (res)
        {
            case "succeeded":
                Model.ExchangeHistoryBase _extobj = new Model.ExchangeHistoryBase();
                _extobj.UserId = this.UserId;
                _extobj.ExchangeCount = int.Parse(Convert.ToString(Math.Ceiling(_obj.TotalPrice)));
                _extobj.ExchangeDate = DateTime.Now;
                _extobj.ExchangeFor = string.Format("<a href=\"shopping_checkout.aspx?orderid={0}&otype=viewner\">{1}</a>", _obj.OrderId, _obj.OrderNumber);
                BLL.ExchangeBase.CreateIntegral(_extobj);

                Response.Redirect(string.Format("shopping_ordersucceed.aspx?orderid={0}", _obj.OrderId));
                break;
            default:
                JScript.AlertAndRedirect(res, RequestBase.GetUrl());
                break;
        }
    }
}
